package final2107.day04;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LambdaDemo03 {
    public static void main(String[] args) {
        //统计字符串中的字符次数,并按次数降序排列
        String str = "abcaaabcccfffff";
        //使用map统计字符的次数
        Map<Character,Integer> map = new HashMap<>();
        char[] ary = str.toCharArray();
        for (char c:ary){
            if (map.containsKey(c)){
                map.put(c,map.get(c)+1);
            }else {
                map.put(c,1);
            }
        }
//        map.forEach((k,v)-> System.out.println(k+":"+v));
        //将map转换为list,对list自定义排序
        List<Map.Entry<Character,Integer>> list =
                new ArrayList<>(map.entrySet());
        list.sort((o1,o2)->o2.getValue().compareTo(o1.getValue()));
        list.forEach(entry-> System.out.println(entry));

    }
}
